package 刷题.leedcode160;

import java.util.HashSet;
import java.util.Set;

//哈希集合
public class Solution1 {
    public ListNode getIntersectionNode(ListNode headA,ListNode headB){
        //使用哈希集合储存一个链表的结点
        //遍历第二条链表，用第二条链表的每个结点去哈希集合中找
        Set<ListNode> visited = new HashSet<>();
        ListNode temp = headA;
        while(temp != null){
            visited.add(temp);
            temp = temp.next;
        }
        temp = headB;
        while(temp != null){
            if(visited.contains(temp)){
                return temp;
            }
            temp = temp.next;
        }
        return null;
    }
}
